/*
 *	(*^。^*) TFrameWork License (*^。^*)
 */

/*
 *  Title :
 *
 *  Description :
 *
 *
 *  Date :
 *
 *  Author : DengPing
 *
 *  Modify :
 */

using UnityEngine;
using UnityEngine.UIElements;
using System.Collections.Generic;

namespace TFrameWork.Module.ActionEditor
{
    public abstract class TrackItemStyleBase
    {
        protected abstract string eventFullName { get; }
        public VisualElement root { get; protected set; }
        public VisualElement select;

        public static Color selectColor = new Color(0.2f, 1, 0.6f, 0.7f);
        public static Color normalColor = new Color(0, 0, 0, 0);

        public abstract TrackEventBase eventData { get; }

        public virtual void SetBGColor(Color color)
        {
            root.style.backgroundColor = color;
        }

        public virtual void SetWidth(float width)
        {
            root.style.width = width;
        }

        public virtual void SetPosition(float x)
        {
            Vector3 pos = root.transform.position;
            pos.x = x;
            root.transform.position = pos;
        }

        /// <summary>
        /// 当前帧是否命中当前事件
        /// </summary>
        /// <param name="currentIndex"></param>
        /// <returns></returns>
        public bool IsHit(int currentIndex)
        {
            return currentIndex >= eventData.startIndex &&
                   currentIndex <= eventData.startIndex + eventData.durationIndex;
        }

        public virtual void Select()
        {
            if (select == null)
            {
                select = root.Q("Select");
                if (select == null)
                {
                    ActionEditorLog.Error("没有Select的组件");
                    return;
                }
            }

            //if (ActionEditor.Instance.currentSelected == this) return;
            if (ActionEditor.Instance.currentSelected != null)
            {
                ActionEditor.Instance.currentSelected.OnUnSelect();
            }

            ActionEditor.Instance.currentSelected = this;

            ActionEditor.Instance.ShowTrackItemOnInspecotr(this);
        }

        #region 左右拖动提示

        protected VisualElement LeftPoint;
        protected VisualElement RightPoint;

        protected void InitLeftRightDrag()
        {
            LeftPoint = root.Q(nameof(LeftPoint));
            RightPoint = root.Q(nameof(RightPoint));

            LeftPoint.RegisterCallback<MouseEnterEvent>(LeftPointMousePointEnter);
            LeftPoint.RegisterCallback<MouseOutEvent>(LeftPointMousePointOut);

            RightPoint.RegisterCallback<MouseEnterEvent>(RightPointMousePointEnter);
            RightPoint.RegisterCallback<MouseOutEvent>(RightPointMousePointOut);
        }

        static Color canDragColor = new Color(0, 1, 0, 0.8f);
        static Color DragNormalColor = new Color(0, 0, 0, 0f);

        private void RightPointMousePointEnter(MouseEnterEvent evt)
        {
            RightPoint.style.backgroundColor = canDragColor;

            //Cursor.SetCursor();
        }

        private void RightPointMousePointOut(MouseOutEvent evt)
        {
            RightPoint.style.backgroundColor = DragNormalColor;
        }

        private void LeftPointMousePointEnter(MouseEnterEvent evt)
        {
            LeftPoint.style.backgroundColor = canDragColor;
        }

        private void LeftPointMousePointOut(MouseOutEvent evt)
        {
            LeftPoint.style.backgroundColor = DragNormalColor;
        }

        #endregion

        public virtual void OnSelect()
        {
            if (select != null)
            {
                select.style.borderLeftWidth = 2;
                select.style.borderRightWidth = 2;
                select.style.borderTopWidth = 2;
                select.style.borderBottomWidth = 2;
                select.style.borderLeftColor = selectColor;
                select.style.borderRightColor = selectColor;
                select.style.borderTopColor = selectColor;
                select.style.borderBottomColor = selectColor;
            }

        }

        public virtual void OnUnSelect()
        {
            if (select != null)
            {
                select.style.borderLeftWidth = 0;
                select.style.borderRightWidth = 0;
                select.style.borderTopWidth = 0;
                select.style.borderBottomWidth = 0;
                select.style.borderLeftColor = normalColor;
                select.style.borderRightColor = normalColor;
                select.style.borderTopColor = normalColor;
                select.style.borderBottomColor = normalColor;
            }
        }

        /// <summary>
        /// 录制节点
        /// </summary>
        public virtual void RecordEvent(int frameIndex)
        {
            if (eventData is RecordTrackEventBase)
            {
                var recordData = eventData as RecordTrackEventBase;
                var preview = ActionEditor.Instance.PreviewCharacterObj;
                if (recordData.recordTransform == null)
                {
                    var t = ActionEditorUtility.FindChildTransformByNameIndex(preview.transform,
                        recordData.rocordTransformName);
                    if (t == null) return;
                    recordData.recordTransform = t;
                }

                Transform recordParentRoot = recordData.recordTransform;

                if (preview == null || recordParentRoot == null) return;
                if (!recordParentRoot.transform.IsChildOf(preview.transform))
                {
                    if (preview.transform != recordParentRoot)
                    {
                        ActionEditorLog.Error("录制对象必须是预览的子对象，或者预览对象本身");
                        return;
                    }
                }

                if (frameIndex == recordData.startIndex)
                {
                    recordData.recordEventDatas = new Dictionary<int, RecordEventData>();
                    recordData.rocordTransformName = recordParentRoot.name;
                    recordData.relativelocalToWrold = new List<Transform>();
                    var parent = recordParentRoot;
                    while (parent != preview.transform)
                    {
                        recordData.relativelocalToWrold.Add(parent);
                        parent = parent.parent;
                    }
                    //recordData.relativelocalToWrold.Add(parent);
                }

                ActionEditorLog.Log("数据录制");
                var recordEventData = new RecordEventData();

                var localPos = Vector3.zero;
                var localRotation = Quaternion.identity;
                var localScale = Vector3.one;
                var localToParent = Matrix4x4.TRS(localPos, localRotation, localScale);
                if (recordData.relativelocalToWrold != null && recordData.relativelocalToWrold.Count > 0)
                {
                    for (int i = 0; i < recordData.relativelocalToWrold.Count; i++)
                    {
                        var parentTrans = recordData.relativelocalToWrold[i];
                        var parentPos = parentTrans.transform.localPosition;
                        var parentRotation = parentTrans.transform.localRotation;
                        var parentScale = parentTrans.transform.localScale;
                        var parentMat = Matrix4x4.TRS(parentPos, parentRotation, parentScale);
                        localToParent = parentMat * localToParent;
                    }
                }

                if (frameIndex == recordData.startIndex + recordData.durationIndex)
                {
                    recordData.relativelocalToWrold = null;
                }
                recordEventData.localToRelativeRootWrold = localToParent;
                recordData.recordEventDatas[frameIndex] = recordEventData;
            }

        }

        /// <summary>
        /// 当前帧命中当前事件时调用
        /// </summary>
        /// <param name="frameIndex"></param>
        public virtual void HitEventPreView(int frameIndex)
        {
            
        }
        
        
        public virtual void TickPreView(int frameIndex)
        {

        }

        public virtual void ResetView()
        {

        }

        public virtual void DrawGizmos()
        {

        }

        public virtual void OnSceneGUI()
        {

        }

        public virtual void OnDestroy()
        {

        }
    }
}
